interface ICarInfoScope extends ng.IScope {
    // 车辆ID；
    carId:string;
    orderAccept_carCode:string;
    orderAccept_carType:string;
    orderAccept_carLength:string;
    orderAccept_bearing:string;
    orderAccept_driver:string;
    orderAccept_identity:string;
    orderAccept_driverPhone:string;
    orderAccept_carrierCategory:string;
    orderAccept_carSource:string;
    orderAccept_permitNumber:string|number;
    areaCode:string;
    countrySubdivisionCode:string;
    cityCode:string;
    provinceCode:string;
    deliverCityList:AreaItem[];
    deliverAreaList:AreaItem[];

    


    orderAccept_roadTransportCertificateNumber:string;
    orderAccept_address:string;


    driverUploader:any;
    trailerUploader:any;
    travelUploader:any;
    idCardUploader:any;
    carPicUploader:any;
         /**
     * 道路运输证
     */
    roadTransportCardUploader:any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader:any;

    realHeadImgUploader:any;
    
    attachList:OssUploaderItem[];

    uploader:any;
    
    heightTradingA:number;
    querying:boolean;

    gridOptions:any;
    gridOptionsRoute:any;
    // 交易记录列表
    gridOptionsTrading:any;
    queryTerms:IOrderAcceptBankCardManagentQueryTerms;
    queryFunc:()=>void;

    maxApp:number;
    currentPage:number;
    totalItemsApp:number;
    totalApp:string|number;
    praiseCountApp:string|number;
    evaluateList:EvaluateListItemResponse[];
    
    total:number|string;

    averageApp:string|number;
    pageChanged:(currentpage:number)=>void;

     /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;

    selectedImgSrc:string;

    imgClick:(item)=>void;



}
interface IOrderAcceptBankCardManagentQueryTerms{
       //交易记录的查询条件
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    goodsNum: string;
    /**
     * 线路地址
     */
    route: string;
    /**
     * 发货地址
     */
    shipAddress: string;
    /**
     * 送货地址
     */
    deliverAddress: string;
}


function tmsCarInfo(locationService: ILocationService, $ngBootbox: BootboxService, carService: carService, commonService: commonService,  routeService: routeService, areaService: areaService,ossUploaderService:ossUploaderService): ng.IDirective {
    return {
        restrict: 'E',
        templateUrl: './src/directives/tms-carInfo/tms-carInfo.html',
        transclude: true,
        scope: {
            carId: "=",
            imgClick:"&"
        },
        link: ($scope: ICarInfoScope) => {
            
            //App竞价详情中的交易记录
            var gridApiTrading: uiGrid.IGridApiOf<TradingRecordListItemResponse>;
            init();
            getInfo();
            
            $scope.handleImgClick=(item)=>{
                // console.log(item);
                $scope.imgClick({item:item})
            }

            // 初始化
            function init() {
                // 资质信息初始化

                const OssFile = ossUploaderService.getOssFile();
                $scope.attachList=[
                    {
                        title:'驾驶证',
                        uploader:null,
                        uploaderName:'driverUploader',

                    },
                    {
                        title:'挂车证',
                        uploader:null,
                        uploaderName:'trailerUploader',

                    },
                    {
                        title:'行驶证',
                        uploader:null,
                        uploaderName:'travelUploader',

                    },
                    {
                        title:'身份证',
                        uploader:null,
                        uploaderName:'idCardUpload',

                    },
                    {
                        title:'车辆照片',
                        uploader:null,
                        uploaderName:'carPicUploader',

                    },
                    {
                        title:'道路运输证',
                        uploader:null,
                        uploaderName:'roadTransportCardUploader',

                    },
                    {
                        title:'道路运输经营许可证',
                        uploader:null,
                        uploaderName:'roadTransportLicenseUploader',

                    },
                    {
                        title:'从业资格证',
                        uploader:null,
                        uploaderName:'occupationalLicenseUploader',

                    },
                    {
                        title:'真实头像',
                        uploader:null,
                        uploaderName:'realHeadImgUploder',

                    }
                ]
                $scope.attachList.forEach((item,index)=>{
                    item.uploader=new OssFile($scope,item.uploader,{uploaderList:[],tag:index+1});
                })

                

               


                //账户信息列表初始化
                $scope.gridOptions = {
                    enableSorting: false,
                    useExternalPagination: true,
                    useExternalSorting: true,
                    enableColumnResizing: true,
                    paginationPageSizes: [10, 20, 30],
                    columnDefs: [
                        { displayName: "开户银行", field: 'bank', enableColumnMenu: false },
                        { displayName: "银行卡号", field: 'cardNumber', minWidth: 300, enableColumnMenu: false },
                        { displayName: "持卡人姓名", field: 'holder', enableColumnMenu: false }
                    ],
                    onRegisterApi: (gridApi) => {
                        gridApi = gridApi;
                        // gridApi.pagination.on.paginationChanged($scope, paginationChanged);
                        gridApi.core.notifyDataChange("options");
                    }
                }

                //主营线路列表初始化
                $scope.gridOptionsRoute = {
                    enableSorting: false,
                    paginationPageSizes: [10, 20, 30],
                    useExternalPagination: true,
                    useExternalSorting: true,
                    enableColumnResizing: true,
                    columnDefs: [
                        { displayName: "承运起始地", field: 'beginAddress', enableColumnMenu: false },
                        { displayName: "承运目的地", field: 'endAddress', minWidth: 300, enableColumnMenu: false },
                        { displayName: "生成日期", field: 'createTime', enableColumnMenu: false }
                    ],
                    onRegisterApi: (gridApi) => {
                        // gridApiRoute = gridApi;
                        // gridApi.pagination.on.paginationChanged($scope, paginationChanged);
                    }
                };
                //交易记录的查询条件
                $scope.queryTerms = {
                    goodsName: "",
                    goodsNum: "",
                    route: "",
                    shipAddress: "",
                    deliverAddress: ""
                };
                //交易记录

                $scope.gridOptionsTrading = {
                    enableSorting: false,
                    paginationPageSizes: [10, 20, 30],
                    useExternalPagination: true,
                    useExternalSorting: true,
                    enableColumnResizing: true,
                    columnDefs: [
                        { displayName: "合同号", field: 'contractNumber', cellTemplate: '<div style="height:{{30 * row.entity.orderList.length}}px;line-height:{{30 * row.entity.orderList.length}}px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{row.entity.contractNumber}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: 88 },
                        { displayName: "发货地址", field: 'originAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.originAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                        { displayName: "送货地址", field: 'destinationAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.destinationAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                        { displayName: '货物名称', field: 'goodsName', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.goodsName}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                        { displayName: '货物数量', field: 'quantityOfGoods', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.quantityOfGoods}}{{item.goodsUnit}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, minWidth: 150 },
                        { displayName: '外付单价', field: 'payablePrice', cellTemplate: '<div style="height:{{30 * row.entity.orderList.length}}px;line-height:{{30 * row.entity.orderList.length}}px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{row.entity.payablePrice}}{{row.entity.payablePriceUnit}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                        { displayName: '发货日期', field: 'realDeliveryTime', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" style="height:30px;line-height:30px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{item.realDeliveryTime}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                    ],
                    onRegisterApi: (gridApi) => {
                        gridApiTrading = gridApi;
                        gridApiTrading.pagination.on.paginationChanged($scope, tradingPaginationChanged);
                    }
                }

                $scope.queryFunc = function () {
                    loadData($scope.queryTerms, 0, 10);
                }

                

                //诚信记录
                $scope.maxApp = 5;
                $scope.currentPage = 1;
                $scope.totalItemsApp = 5;
                $scope.pageChanged = (page: number)=> {
                    var currentPage = (page - 1) * $scope.maxApp;
                    getEvaluateApp($scope.carId, "", "", currentPage, 5);
                };

                
            }

            function getInfo() {
                $scope.carId && getBasciInfo();
            }

            // 获取基本信息 和 资质信息
            function getBasciInfo() {
                carService.getDetail($scope.carId).then(res => {
                    // console.log('res.data', res.data)
                    $scope.orderAccept_carCode = res.data.carCode;
                    $scope.orderAccept_carType = res.data.carType;
                    $scope.orderAccept_carLength = res.data.carLength;
                    $scope.orderAccept_bearing = res.data.bearing;
                    $scope.orderAccept_driver = res.data.driver;

                    $scope.orderAccept_identity = res.data.identity;
                    $scope.orderAccept_driverPhone = res.data.driverPhone;
                    $scope.orderAccept_carrierCategory = res.data.carriageType;
                    $scope.orderAccept_carSource = res.data.carSource;
                    $scope.orderAccept_permitNumber = res.data.permitNumber;

                    //所属辖区
                    $scope.countrySubdivisionCode = res.data.countrySubdivisionCode;
                    let countrySubdinvisionCode = $scope.countrySubdivisionCode;
                    if ($scope.countrySubdivisionCode) {
                        $scope.provinceCode = countrySubdinvisionCode.substring(0, 2);
                        $scope.cityCode = countrySubdinvisionCode.substring(0, 4);
                        $scope.areaCode = countrySubdinvisionCode.substring(0, 6);
                        getProvinceList();
                    }

                    //道路运输证字号
                    $scope.orderAccept_roadTransportCertificateNumber = res.data.roadTransportCertificateNumber;
                    //地址
                    $scope.orderAccept_address = res.data.address;


                    // 附件信息
                    //附件信息
                    
                   $scope.attachList[0].uploader.addByPath(res.data.drivingLicenseImageKey);
                   $scope.attachList[1].uploader.addByPath(res.data.trailerLicenseKey);
                   $scope.attachList[2].uploader.addByPath(res.data.vehicleLicenseKey);
                   $scope.attachList[3].uploader.addByPath(res.data.identityCardImageKey);
                   $scope.attachList[4].uploader.addByPath(res.data.vehiclePhotoKey);
                   $scope.attachList[5].uploader.addByPath(res.data.roadTransportKey);
                   $scope.attachList[6].uploader.addByPath(res.data.businessLicenseKey);
                   $scope.attachList[7].uploader.addByPath(res.data.qualificationKey);
                   $scope.attachList[8].uploader.addByPath(res.data.certificateImageKey);



                    //账户信息
                    $scope.gridOptions.data = res.data.bankList;
                    $scope.gridOptions.totalItems = $scope.gridOptions.data.length

                    //主营线路
                    $scope.gridOptionsRoute.data = res.data.routeList;
                    $scope.gridOptionsRoute.totalItems = $scope.gridOptionsRoute.data.length;

                    loadData($scope.queryTerms, 0, 10);
                    //评分
                    $scope.averageApp = String(res.data.average);
                    //好评数量
                    $scope.praiseCountApp = res.data.praiseCount;
                    //获取诚信记录信息
                    getEvaluateApp($scope.carId, "", "", 0, 5);




                })
            }

            /**
           * 获取市区下拉
           */
            function getProvinceList() {

                if ($scope.provinceCode) { 
                    areaService.getCity($scope.provinceCode).then(res => {
                        $scope.deliverCityList = res.data.data;
    
                    });
                 };
               
                if ($scope.cityCode) {  
                    areaService.getCounty($scope.cityCode).then(res => {
                        $scope.deliverAreaList = res.data.data;
                    });
                };
                
            }

            //APP竞价----车辆详情中的交易记录的查询
            function loadData(queryTerms: IOrderAcceptBankCardManagentQueryTerms, skip: number, count: number): void {


                //交易记录
                carService.getTradingRecordList($scope.carId, "", queryTerms.goodsName, queryTerms.goodsNum,
                    queryTerms.route, queryTerms.shipAddress, queryTerms.deliverAddress, skip, count).then(result => {
                        // $scope.gridOptionsTrading.totalItems = result.data.total;
                        $scope.gridOptionsTrading.data = result.data.data;
                        // $scope.gridOptionsTrading.totalItems = result.data.data.length;
                        console.log('trading', result.data.data)
                        /**
                         * 设置列表高度
                         */
                        let gridHArr = 0;
                        result.data.data.forEach((item, index) => { gridHArr = gridHArr + item.orderList.length; });
                        $scope.heightTradingA = (gridHArr * 30) + 96;
                        if (skip == 0) {
                            $scope.gridOptions.paginationCurrentPage = 1;
                        }
                        $scope.querying = false;
                    }, error => {
                        $scope.querying = false;
                    });

                carService.getTradingRecordCount($scope.carId, "", queryTerms.goodsName, queryTerms.goodsNum,
                    queryTerms.route, queryTerms.shipAddress, queryTerms.deliverAddress).then(res => {
                        $scope.total = String(res.data.extData);
                        $scope.gridOptionsTrading.totalItems = res.data.extData;
                    })
            }

            function tradingPaginationChanged(newPage: number, pageSize: number) {
                loadData($scope.queryTerms, newPage, pageSize);
            }

            //诚信记录
            function getEvaluateApp(driveruserId, carCode, appraiserid, skip: number, count: number): void {
                carService.getEvaluateList(driveruserId, "", "", skip, count).then((res) => {
                    res.data.data.map((item, index) => { item.index = (skip * 10) + (index + 1) });
                    $scope.totalApp = res.data.total;
                    $scope.evaluateList = res.data.data;
                    $scope.totalItemsApp = res.data.total;
                    $scope.evaluateList.forEach(item=>{
                        item['credibilityStar']=item.credibility/2;
                        item['attitudeStar']=item.attitude/2;
                        item['timelinessStar']=item.timeliness/2;
                        item['freightStar']=item.freight/2;
                        item['averageStar'] = item.average/2;
                        
                    })
                });
            }

            



            $scope.$watch('carId', (newVal, oldVal) => {
                getInfo()
            })


        }

    }
}


tmsCarInfo['$inject'] = ["locationService", "$ngBootbox", "carService", "commonService", "routeService", "areaService","ossUploaderService"]
angular.module('tmsCarInfo', []).directive('tmsCarInfo', tmsCarInfo);